package com.taotao.web.controller;

import org.apache.commons.io.IOUtils;

import java.net.URL;
import java.net.URLConnection;
import java.util.List;

/**
 * author: 王明跃
 * Create at: 18/7/13上午8:54
 */
public class Cross {
    // 请求路径
    public static final String DURL = "http://172.16.16.105:8080/dsolr/delete";
    public static final String UURL = "http://172.16.16.105:8080/usolr/update";

    public static String deleteIds(String url, Long[] ids) {
        StringBuilder sb = new StringBuilder();
        for (Long id : ids) {
            sb.append(id).append(",");
        }
        String id = sb.toString().substring(0, sb.toString().length() - 1);
        String result = "";
        try {
            result = deleteSolr(url, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    // pdata 请求参数
    private static String deleteSolr(String url1, String ids) throws Exception {
        //用url路径以及所用参数创建URL实例类
//        URL url = new URL(urlString + "?" + pdata);
        //用url路径以及所用参数创建URL实例类

        URL url = new URL(url1 + "?ids=" + ids);
        //创建连接
        URLConnection connect = url.openConnection();
        //设置请求header的属性--请求内容类型
        connect.setRequestProperty("content-type",
                "application/x-www-form-urlencoded;charset=utf-8");
        //设置请求header的属性值--请求方式
        connect.setRequestProperty("method", "GET");
        //建立与url所在服务器的连接(访问该接口)
        connect.connect();
        List<String> strings = IOUtils.readLines(connect.getInputStream());
        StringBuilder sb = new StringBuilder();
        for (String string : strings) {
            sb.append(string);
        }
        // 返回值(一般是json数据)
        String result = sb.toString();
        return result;
    }

    private static void post(Long[] ids) throws Exception {
        URL url = new URL(DURL);
        // 打开链接
        URLConnection connect = url.openConnection();
        connect.setRequestProperty("content-type",
                "application/x-www-form-urlencoded;charset=utf-8");
        connect.setRequestProperty("method", "POST");
        connect.setDoOutput(true);
        connect.setDoInput(true);
        // 发送请求参数
//        IOUtils.write(ids, connect.getOutputStream());
        // 获取URL请求的结果
        List<String> strings = IOUtils.readLines(connect.getInputStream());
        StringBuilder sb = new StringBuilder();
        for (String string : strings) {
            sb.append(string);
        }
        // 返回值(一般是json数据)
        String result = sb.toString();
        System.out.println(result);
    }

}
